package com.sys.app.entity.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;


/**
 * 加密帮助类
 */
public class SecrecyUtil {
    private static Logger logger = LoggerFactory.getLogger(SecrecyUtil.class);
    /**
     * MD5加密
     * @param ciphertext 机密字符传
     * @param key 机密盐,没有可输入null
     * @return 返回机密字符串(小写)
     */
    public static String decryptMD5(String ciphertext, String key){
        if(StringUtil.isEmpty(key)){
            return DigestUtils.md5Hex(ciphertext);
        }else {
            return DigestUtils.md5Hex(String.format("%s%s", ciphertext, key));
        }
    }

    /**
     *AES加密解密key不够16字符长度自动补齐零
     * @param key
     * @return
     */
    public static String getAESKey(String key){
        String res=String.format("%16s",key);
        return res.replaceAll("\\s","0");
    }
    /**
     * 加密
     * @param encryptStr 加密的字符串
     * @param key key值
     * @return
     * @throws Exception
     */
    public static String encryptAES(String encryptStr, String key)  {
        try {
            if(StringUtil.isEmpty(key)){
                logger.error("encryptAES加密key不允许为空!");
                return null;
            }
            if(key.length()!=16){
                logger.error("encryptAES加密key长度不是16位!");
                return null;
            }

            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "AES"));
            byte[] b = cipher.doFinal(encryptStr.getBytes(Charset.forName("UTF-8")));
            // 采用base64算法进行转码,避免出现中文乱码
            return Base64.encodeBase64String(b);
        }catch(Exception ex){
            logger.error("encryptAES({},{})加密异常", encryptStr, key,ex);
            return null;
        }
    }

    /**
     * 解密
     * @param decryptStr 解密的字符串
     * @param key 解密的key值
     * @return
     * @throws Exception
     */
    public static String decryptAES(String decryptStr, String key) {
        try {
            if(StringUtil.isEmpty(key)){
                logger.error("decryptAES解密key不允许为空!");
                return null;
            }
            if(key.length()!=16){
                logger.error("decryptAES解密key长度不是16位!");
                return null;
            }
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "AES"));
            // 采用base64算法进行转码,避免出现中文乱码
            byte[] encryptBytes = Base64.decodeBase64(decryptStr);
            byte[] decryptBytes = cipher.doFinal(encryptBytes);
            return new String(decryptBytes, Charset.forName("UTF-8"));
        }catch(Exception ex){
            logger.error("decryptAES({},{})解密异常", decryptStr, key,ex);
            return null;
        }
    }




    /**
     *
     * @param code 账户
     * @param pwd1 前端密码
     * @param pwd2 数据库查询返回密码
     * @return 密码一致返回true,否则返回false
     */
   public static boolean LoginCheckPwd(String code,String pwd1,String pwd2,String key){
        String pwdInput=String.format("%s%s%s",code,pwd1,key);
        if(pwdInput.equals(pwd2)){
            return false;
        }else{
            return false;
        }
   }

    /**
     * 账号根据用户名+密码+盐 加密
     * @param code 用户名
     * @param pwd 密码
     * @return
     */
   public static String UserPwdMd5(String code,String pwd,String key){
       return decryptMD5(String.format("%s%s",code,pwd),key);
   }
}
